package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.AreaService;
import com.yixun.qfbao.apidto.GetProvinceReqDto;
import com.yixun.qfbao.apidto.GetProvinceResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetProvinceReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 获取省份
 * @author guifang.weng
 * @create 2017-11-23 13:48
 **/
@Service("getProvinceHander")
public class GetProvinceHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetProvinceHander.class);

    @Autowired
    private AreaService areaService;
    
    @Autowired
	private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
    	GetProvinceReqVo req = (GetProvinceReqVo)reqDto;
    	GetProvinceReqDto reqDto1 = req.getParams();
        BaseResponseDTO<GetProvinceResDto> res = new BaseResponseDTO<>();
        try {
        	GetProvinceResDto resDto = new GetProvinceResDto();
        	//从缓存读取省份列表，读取不到时，使用查询方法，再存入缓存
			Object object = loginUtils.get("province_" + reqDto1.getCompanyUuid());
			if(object != null) {
				resDto = (GetProvinceResDto) object;
				resDto.setResult(ResultEnum.SUCCESS.getNo());
			} else {
				resDto = areaService.getProvince();
				loginUtils.put("province_" + reqDto1.getCompanyUuid(), resDto);
			}
            res.setResult(resDto.getResult());
            res.setResData(resDto);
        } catch (Exception e) {
            logger.error("GetProvinceHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("获取省份列表失败");
        }
        return res;
    }
}
